package edu.westga.cs3152.wordmojo;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;

/**
 * The Dictionary reads in a file of and keeps track of a list of valid English
 * words. It also provides methods to access this list and see if words are
 * contained within the dictionary.
 * 
 * @author Lewis Williams
 * @version 2011.5.3
 */
public class Dictionary {
	
	//The Dictionary Hashtable
	private Hashtable<Integer, String> dictionary = new Hashtable<Integer, String>();
	
	/**
	 * A default constructor for Dictionary that does nothing.
	 */
	public Dictionary(){}

	/**
	 * A constructor for Dictionary that accepts the file path for a source file for 
	 * the words and calls populateDictionary to add these words to the hash table.
	 * @param sourceFile
	 * 			The file path of the file containing the words to be added to the dictionary.
	 */
	public Dictionary(String sourceFile){
		populateDictionary(sourceFile);
	}

	/**
	 * Populates the dictionary hashtable by reading in a file containing a list of words.
	 * 
	 * @param sourceFile
	 * 			The file containing the words to be added to the dictionary.
	 */
	private void populateDictionary(String sourceFile) {
		try{
    		//Creates the file
    		File words = new File(sourceFile);
    		//Creates the scanner to read the file.
    		Scanner scan = new Scanner(words);
    		//The word currently being read in from the file
    		String currentWord = "";
    		//Populates hashtable by reading in and parsing the lines of the file.
    		while(scan.hasNext()){
    			currentWord = scan.nextLine();
    			dictionary.put(currentWord.hashCode(), currentWord);
    		}
    	}
    	//Catches FileNotFoundException if the board file cannot be found.
    	catch(FileNotFoundException fnfe){
    		System.out.println("The dictionary file cannot be found");
    	}				
	}
	
	public boolean contains(String word){
		return dictionary.containsKey(word.hashCode());
	}
	
	/**
	 * Very inefficiently returns a three letter word from the dictionary 
	 * 
	 * (Sorry! I ran out of time. I promise I'm not stupid!)
	 * 
	 * @return
	 */
	public String returnThreeLetterWord(){
		String threeLetterWord = "";
		String words = dictionary.toString();
		while(threeLetterWord.length() != 3){
			int beginningIndex = words.indexOf('=', (int)(Math.random() * 1226770)) + 1;
			try{
			threeLetterWord = dictionary.toString().substring(beginningIndex, words.indexOf(',', beginningIndex));
			}
			catch(Exception iooe){
				threeLetterWord = "hat";
			}
		}
		return threeLetterWord;
	}
	
	/**
	 * Very inefficiently returns a four letter word from the dictionary 
	 * 
	 * (Sorry! I ran out of time. I promise I'm not stupid!)
	 * 
	 * @return
	 */
	public String returnFourLetterWord(){
		String fourLetterWord = "";
		String words = dictionary.toString();
		while(fourLetterWord.length() != 4){
			int beginningIndex = words.indexOf('=', (int)(Math.random() * 1226770)) + 1;
			try{			
			fourLetterWord = dictionary.toString().substring(beginningIndex, words.indexOf(',', beginningIndex));
			}
			catch(Exception iooe){
				fourLetterWord = "sure";
			}
		}
		return fourLetterWord;
	}
	
}